<?php

// $Id$

/**
 * Controller_Telephonebook 控制器
 */
class Controller_Telephonebook extends Controller_Abstract
{
    function actionIndex()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        $cur_user = $this->_app->currentUserObject();

        if($cur_user->permission_id != 2)
        {
            return $this->_redirect(url('default/index'));
        }

        $page = intval($this->_context->page);
        if ($page < 1) $page = 1;
        //$select = Telephonebook::find('enterprise_id = ?', $cur_user->enterprise_id)->order('tel_sequence ASC')->all();;
        $select = User::find('enterprise_id  = ? AND permission_id <> 2 AND permission_id <> 1', $cur_user->enterprise_id)->order('user_position ASC')->all();
        $select->limitPage($page, 10);
        // 将分页信息和查询到的数据传递到视图
        $this->_view['pagination'] = $select->getPagination();
        $this->_view['books'] = $select->getAll();
        
    }

    function actionaddsignaluser()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        $cur_user = $this->_app->currentUserObject();

        //用户所在企业的部门
        $deps = Department::find('enterprise_id = ?', $cur_user->enterprise_id)->all();

        $dep = Userindepartment::find('user_id = ?', $this->_context->user_id)->query();
        $this->_view['dep_id'] = $dep->department_id;
        $this->_view['deps'] = $deps->getAll();
        $this->_view['depsCount'] = $deps->getCount();        


        $this->_view['current_user'] = $cur_user;
        $this->_viewname = 'addsignaluser';
    }

    function actionAdd()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        $cur_user = $this->_app->currentUserObject();
        if($cur_user->permission_id != 2)
        {
            return $this->_redirect(url('default/index'));
        }


        $tel_name = $this->_context->tel_name;
        $tel_num = $this->_context->tel_num;
        $tel_sequence = $this->_context->tel_sequence;

        //return $tel_name.$tel_num.$tel_sequence;
        $tel_user = new Telephonebook();
        $tel_user->enterprise_id = $cur_user->enterprise_id;
        $tel_user->tel_name=$tel_name;
        $tel_user->tel_num=$tel_num;
        //$tel_user->abb = $this->getinitial($tel_name);

        if($tel_sequence != 0)
        {
            $tel_user->tel_sequence=$tel_sequence;
        }
        else
        {
            $tel_user->tel_sequence=100;
        }
        $tel_user->save();
        echo $tel_user->id();
        return 0;
    }

    function actionDelete()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        $cur_user = $this->_app->currentUserObject();
        if($cur_user->permission_id != 2)
        {
            return $this->_redirect(url('default/index'));
        }

        $user = $this->_app->currentUserObject();

        if(isset($_POST['checkboxid'])){
            $id=$_POST['checkboxid'];

            foreach ($id as $value){
                Telephonebook::meta()->destroyWhere('telephonebook_id = ?', $value);
            }
        }

        return $this->_redirect(url('Telephonebook/index'));
    }

    function actionEdit()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        $cur_user = $this->_app->currentUserObject();

        if($cur_user->permission_id != 2)
        {
            return $this->_redirect(url('default/index'));
        }

        $tels = Telephonebook::find('telephonebook_id = ?', $this->_context->telephonebook_id)->query();

        if($tels->id() != $this->_context->telephonebook_id)
        {
            return $this->_redirect(url('Telephonebook/index'));
        }

        if(isset($_POST['tel_name']))
        {
            $tels->tel_name =$_POST['tel_name'];
            $tels->tel_num =$_POST['tel_num'];
            $tels->tel_sequence =$_POST['tel_sequence'];

            if(isset($_POST['department_id']))
            {
                $tels->department_id = $_POST['department_id'];
            }
            if(isset($_POST['position']))
            {
                $tels->position = $_POST['position'];
            }

            $tels->save();
            return $this->_redirectMessage(
                '提示您:',
                '编辑成功',
                url('Telephonebook/index'));
        }

        //用户所在企业的部门
        $deps = Department::find('enterprise_id = ?', $cur_user->enterprise_id)->all();
        $this->_view['deps'] = $deps->getAll();
        $this->_view['depsCount'] = $deps->getCount();

        $this->_view['tel'] = $tels;
        $this->_viewname = 'edit';
    }

    function actionPhoneRequireTels()
    {
        $user_name = $this->_context->user_name;
        if(!isset($this->_context->user_name))
        {
            return 'failed';
        }
        $user = User::find('username = ?', $user_name)->query();
        if(!isset($user->username) || $user->username != $user_name)
        {
            return 'failed';
        }

        //$select = User::find('enterprise_id  = ?', $user->enterprise_id)->order('tel_sequence ASC')->order("CONVERT(tel_name USING gbk)")->all();
        $select = User::find('enterprise_id  = ? AND permission_id <> 2 AND permission_id <> 1 AND user_status != -1' , $user->enterprise_id)->order('user_position ASC')->all();

        $tels = $select->getAll();

        require 'genrss.php';
        $date = Date('Y-m-d H:i:s', time()); 

        $rss_gen=new RSS("企业电话本", "...", '....', '....');
        //echo "##########";
        foreach($tels as $tel)
        { 
            $dep = Userindepartment::find('user_id = ?', $tel->user_id)->query();
            $dep_name = "默认部门";
            if($dep->department_id != -1)
            {
                $dep_info = department::find('department_id  = ?', $dep->department_id )->query();
                if($dep_info->department_id ==  $dep->department_id && isset($dep_info->department_name) && strlen($dep_info->department_name) > 0)
                {
                    $dep_name = $dep_info->department_name;
                }
            }
            if(strlen($tel->user_realname) < 1)
            {
                $tel->user_realname = $tel->username;
            }
            if(strlen($tel->user_cellphone) < 1)
            {
                $tel->user_cellphone = $tel->username;
            }
            $rss_gen->AddItem_Tel($tel->user_realname, 
                $dep->department_id, 
                $dep_name, 
                $tel->user_cellphone, 
                $tel->user_hometel,  
                $tel->user_officetel , 
                $tel->user_position,
                $tel->reserved_1);
        }
        
        $ret=$rss_gen->FetchUserTel();
        return $ret;
    }
}


